package cn.tedu.mr;

import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;

import java.io.IOException;

/*
---------------Reduce---------------
    Text: k2 词
    IntWritable: v2 出现的一次
    Text: k3 词
    IntWritable: v3 一共出现的次数

 */
public class WordReducer extends Reducer<Text, IntWritable,Text,IntWritable> {
    /**
     *
     * @param key: k2 词
     * @param values: v2 按照词进行聚合(1,1,1,1,1...)
     * @param context:
     */
    @Override
    protected void reduce(Text key, Iterable<IntWritable> values, Context context) throws IOException, InterruptedException {
        //将聚合后的数据累加求和:hello(1,1,1,1,1,1) => hello 6
        int count = 0;
        for(IntWritable value : values){
            count += value.get();
        }
        //写出数据
        context.write(key,new IntWritable(count));
    }
}
